package com.fruitshop.manager;

import com.fruitshop.manager.base.BaseManager;
import com.fruitshop.mapper.UserMapper;
import com.fruitshop.model.Command;
import com.fruitshop.model.User;
import com.fruitshop.util.ConsoleUtils;
import com.fruitshop.util.DBUtil;
import com.fruitshop.util.PrintUtils;

import java.util.List;
import java.util.Scanner;

public class UserRegisterManager extends BaseManager {

    private static final String USER_INPUT="1";

    public UserRegisterManager(String title, List<Command> commandList) {
        super(title, commandList);
    }


    @Override
    protected void printTitleExtra() {




        User user=new User();
    }

    @Override
    protected String processCommand(String command) {

        switch (command) {
            case USER_INPUT:
                return showUserInput();
        }
        return command;
    }

    private String showUserInput() {
        Scanner scanner = new Scanner(System.in);
        String name=ConsoleUtils.readString(scanner, "请输入用户名");
        String account=ConsoleUtils.readString(scanner, "请输入账号");
        String password="";
        while (true){
            //输入密码
             password=ConsoleUtils.readString(scanner, "请输入密码");
            //输入确认密码
            String rePassword = ConsoleUtils.readString(scanner, "请输入确认密码");
            if(password.equals(rePassword)){
                break;
            }
            PrintUtils.printError("两次输入的密码不一致,请重新输入");
        }

        int age = ConsoleUtils.readInt(scanner, "请输入年龄区间在(0,100]之间的证整数", value -> value>0&&value<=100);
        User user = new User(name, account, password, age);

        UserMapper userMapper = DBUtil.getMapper(UserMapper.class);

        User byAccount = userMapper.selectByAccount(account);

        if(byAccount!=null){
            PrintUtils.printError("该账号已存在");
            return NORMAL_EXIT;
        }
        userMapper.insert(user);

        PrintUtils.printHighLight("注册成功");

        return NORMAL_EXIT;
    }
}
